package com.jdls.tanhua.web;

import com.jdls.tanhua.mongo.Movement;
import com.jdls.tanhua.server.MovementsServer;
import com.jdls.tanhua.vo.FateValueVo;
import com.jdls.tanhua.vo.MovementsVo;
import com.jdls.tanhua.vo.PageResult;
import com.jdls.tanhua.vo.TodayBest;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/10/27
 */
@RestController
@RequestMapping("/movements")
public class MovementsWeb {

    @Autowired
    private MovementsServer movementsServer;

    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementsServer.save(movement,imageContent);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/all")
    public ResponseEntity all(Long userId,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize){

        PageResult pageResult = movementsServer.all(userId,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping
    //查询好友动态
    public ResponseEntity findOne(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize){

        PageResult pageResult = movementsServer.Friend(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }


    @GetMapping("/recommend")
    //查询推荐动态
    public ResponseEntity recommend (@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize){

        PageResult pageResult = movementsServer.randomFind(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/{id}")
    //查询单条动态
    public ResponseEntity findOne(@PathVariable ObjectId id){
        MovementsVo movementsVo = movementsServer.findOne(id);

        return ResponseEntity.ok(movementsVo);
    }

    @GetMapping("/{id}/like")
    //动态点赞
    public ResponseEntity like(@PathVariable String id){
        Integer like = movementsServer.likeCount(id);
        return ResponseEntity.ok(like);
    }


    @GetMapping("/{id}/dislike")
    //取消点赞
    public ResponseEntity disLike(@PathVariable String id){
        Integer like = movementsServer.disLikeCount(id);
        return ResponseEntity.ok(like);
    }

    @GetMapping("/{id}/love")
    //动态喜欢
    public ResponseEntity love(@PathVariable String id){
        Integer love = movementsServer.loveCount(id);
        return ResponseEntity.ok(love);
    }

    @GetMapping("/{id}/unlove")
    //取消喜欢
    public ResponseEntity disLove(@PathVariable String id){
        Integer love = movementsServer.disLoveCount(id);
        return ResponseEntity.ok(love);
    }

    @GetMapping("/visitors")
    //主页谁看过我
    public ResponseEntity visitors(){

        List<FateValueVo> fateValueVos = movementsServer.visitors();
        return ResponseEntity.ok(fateValueVos);
    }
}
